#include "widget_ind.h"
#include "widget.h"

//=====================================================================
//                  PhotonClass - to pass to Widget  
//=====================================================================

static PyMemberDef  WidgetInd_members [] =
{
  {NULL}  // Sentinel 
};

static PyMethodDef  WidgetInd_methods [] =
{
  {NULL}  // Sentinel 
};

static PyTypeObject WidgetIndType = 
{
  PyObject_HEAD_INIT(NULL)
  0,                         // ob_size 
  "ph.WidgetInd",            // tp_name
  sizeof(WidgetInd),         // tp_basicsize 
  0,                         // tp_itemsize
  (destructor)WidgetInd_dealloc,// tp_dealloc
  0,                         // tp_print 
  0,                         // tp_getattr 
  0,                         // tp_setattr
  0,                         // tp_compare 
  0,                         // tp_repr
  0,                         // tp_as_number
  0,                         // tp_as_sequence 
  0,                         // tp_as_mapping 
  0,                         // tp_hash 
  0,                         // tp_call
  0,                         // tp_str
  0,                         // tp_getattro
  0,                         // tp_setattro
  0,                         // tp_as_buffer
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags 
  "Photon wrapper objects",  // tp_doc 
  0,                         // tp_traverse 
  0,                         // tp_clear 
  0,                         // tp_richcompare 
  0,                         // tp_weaklistoffset 
  0,                         // tp_iter 
  0,                         // tp_iternext 
  WidgetInd_methods,         // tp_methods 
  WidgetInd_members,         // tp_members
  0,                         // tp_getset 
  0,                         // tp_base 
  0,                         // tp_dict
  0,                         // tp_descr_get 
  0,                         // tp_descr_set 
  0,                         // tp_dictoffset 
  (initproc)WidgetInd_init,  // tp_init
  0,                         // tp_alloc 
  WidgetInd_new              // tp_new 
};

static void WidgetInd_dealloc (WidgetInd* self)
{
  self->ob_type->tp_free ((PyObject*) self);
}

static PyObject* WidgetInd_new (PyTypeObject* type, PyObject* args)
{
  Widget* self;

  self = (Widget*) type->tp_alloc (type, 0);
  return (PyObject*) self; 
}

static int WidgetInd_init (WidgetInd* self, PyObject* args)
{
  const char* repr;
  static const char* widget_repr [] = 
  {
    "window",
    "button",
    "group",
    "field",
    "menu",
    "menu_button"
  };

  PtWidgetClassRef_t* widget_class [] =
  {
    PtWindow,
    PtButton,
    PtGroup,
    PtText,
    PtMenu,
    PtMenuButton
  };

  unsigned num;

  if (PyArg_ParseTuple (args, "s", &repr))
  {
    for (num = 0; num < sizeof (widget_repr) / sizeof (widget_repr [0]); ++num)
      if (!strcmp (repr, widget_repr [num]))
      {
        self->widget_class = widget_class [num];
        return 0;
      }

    PyErr_SetString (PyExc_TypeError, "Widget type not recognized");
    return -1;
  }
  else
  {
    PyErr_SetString (PyExc_TypeError, "Widget type must be represented as string");
    return -1;
  }
}

//=====================================================================
//                Module declaration functions
//=====================================================================

static PyMethodDef module_methods [] = 
{
  {NULL, NULL, 0, NULL}
};

#if 0
PyMODINIT_FUNC 
initph_api_widget_ind (void)
{
  PyObject *m;

  if (PyType_Ready (&WidgetIndType) < 0)
    return;

  m = Py_InitModule3 ("ph_api_widget_ind", module_methods, 
      "Photon API extension module");

  if (m == NULL)
    return;

  Py_INCREF (&WidgetIndType);
  PyModule_AddObject (m, "WidgetInd", (PyObject*)&WidgetIndType);
}
#endif

INIT_FUNC (ph_api_widget_ind, WidgetInd);
